<?php

class Soul_Util_FS {
	
	public static function getBaseName($path){
		$result = array();
		$paths = Soul_Util_Array::pack($path);
		foreach($paths as $item){
			$result[] = basename($item);
		}
		if(is_array($path)){
			return $result;
		}
		return $result[0];
	}

    public static function makeDirectory($path) {
        if (!is_dir($path)) {
            mkdir($path, 0700, TRUE);
        }
    }
    
    public static function makeRandomDirectory($basepath, $length = 8) {
		do{
			$result = $basepath.Soul_Util_Random::randomCharacters($length);
		}while(is_dir($result));
		self::makeDirectory($result);
		return $result;
    }    
    
    public static function moveFile($file, $dir, $deleteSource = FALSE){
    	self::makeDirectory($dir);
    	$files = Soul_Util_Array::pack($file);
    	foreach($files as $file){
    		if($file != $dir.DS.basename($file)){
	    		@copy($file, $dir.DS.basename($file));
	    		if($deleteSource){
	    			@unlink($file);
	    		}
    		}
    	}
    }
    
    private static function removeSubDirectories($path){
        if(is_dir($path)){
            $dh = opendir($path);
            while ($file = readdir($dh)) {
                if ($file[0] != ".") {
                    $fullpath = $path.$file;
                    if (is_dir($fullpath)) {
                        self::removeSubDirectories($fullpath.DS);
                        rmdir($fullpath);
                    } else {
                        if(file_exists($fullpath)){
                            unlink($fullpath);
                        }
                    }
                }
            }
            closedir($dh);
        }
    }    
    
    public static function removeDirectory($path){
        self::removeSubDirectories($path);
        rmdir($path);
    }
    
    public static function deleteFile($file){
    	$files = Soul_Util_Array::pack($file);
    	foreach($files as $file){
            if(file_exists($file)){
                unlink($file);
            }
    	}
    }
    
    public static function deleteFileIn($path, $file){
    	$files = Soul_Util_Array::pack($file);
    	foreach($files as $filename){
    		self::delete($path.$filename);
    	}
    }

    public static function getDirectories($path, $recursion = FALSE) {
        $result = array();
        if (is_dir($path)) {
            $handle = opendir($path);
            if ($handle) {
                while (false !== ($dir = readdir($handle))) {
                    if ($dir[0] != "." && is_dir($path . DIRECTORY_SEPARATOR . $dir)) {
                        $arr = array('name' => $dir, 'path' => str_replace('/', DIRECTORY_SEPARATOR, $path . DIRECTORY_SEPARATOR . $dir), 'subDirectory' => array());
                        if ($recursion) {
                            $arr['subDirectories'] = self::getDirectories($path . DIRECTORY_SEPARATOR . $dir, $recursion);
                        }
                        $result[] = $arr;
                    }
                }
                closedir($handle);
            }
        }
        return $result;
    }

    public static function getFiles($path, $type = '*') {
        $result = array();
        if (is_dir($path)) {
            $handle = opendir($path);
            if ($handle) {
                while (false !== ($dir = readdir($handle))) {
                    if ($dir[0] != "." && is_file($path . DIRECTORY_SEPARATOR . $dir)) {
                        $info = pathinfo($path . DIRECTORY_SEPARATOR . $dir);
                        $info['name'] = substr($dir, 0, strpos($dir, '.'));
                        $info['filename'] = $path . DIRECTORY_SEPARATOR . $dir;
                        if (is_array($type)) {
                            if (in_array($info['extension'], $type)) {
                                $result[] = $info;
                            }
                        } else {
                            if ($type == '*' || $info['extension'] == $type) {
                                $result[] = $info;
                            }
                        }
                    }
                }
                closedir($handle);
            }
            return $result;
        }
    }

    public static function findFile(array $directories, $subPath){
        if(substr($subPath, 0, 1) == DIRECTORY_SEPARATOR){
            $subPath = substr($subPath, 1);
        }
        foreach($directories as $directory){
            $dir = substr($directory, -1) == DIRECTORY_SEPARATOR? substr($directory, 0, -1): $directory;
            if(file_exists($dir.DIRECTORY_SEPARATOR.$subPath)){
                return $dir.DIRECTORY_SEPARATOR.$subPath;
            }
        }
        return NULL;
    }
    
    public static function readFile($path){
        $file = fopen($path,'r') or die("Can't open file");
        return file_get_contents($path);
    }

    public static function writeFile($data, $filename, $append = FALSE){
        if(!file_exists($filename)){
            self::makeDirectory(dirname($filename));
        }
        
        if($append){
        	file_put_contents($filename, $data, FILE_APPEND);
        }else{
        	file_put_contents($filename, $data);
        }
    }
    
    public static function mergePath(){
    	return implode(DIRECTORY_SEPARATOR, func_get_args());
    }
}